package com.youmu.sichuansfc.service.impl;

import com.youmu.sichuansfc.dao.NotifyDao;
import com.youmu.sichuansfc.dao.NotifyReadDao;
import com.youmu.sichuansfc.domain.YoumuNotify;
import com.youmu.sichuansfc.domain.YoumuNotifyReadHistory;
import com.youmu.sichuansfc.dto.PageT;
import com.youmu.sichuansfc.service.NotifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;

/**
 * @author WYZ
 * @create 2018-02-01 10:02
 * @desc
 **/
@Service
@Transactional
public class NotifyServiceImpl  implements NotifyService{

    @Autowired
    NotifyDao notifyDao;
    @Autowired
    NotifyReadDao notifyReadDao;

    @Override
    public YoumuNotify upload(YoumuNotify youmuNotify) {
      return  notifyDao.upload(youmuNotify);
    }


    @Override
    public PageT findBy(Boolean isRead, String title, Long fundUserId, int num, int size, int type) {

        return  notifyDao.findBy(isRead, title, fundUserId, num, size,  type);
    }

    @Override
    public PageT findBy(Date startTime, Date endTime, String title, int num, int size, int type) {
        return notifyDao.findBy(startTime, endTime, title, num, size,  type);
    }


    @Override
    public YoumuNotifyReadHistory readNotify(Long notifyId, Long fundUserId) {
        // 阅读次数 加1
        YoumuNotify youmuNotify =  findById(notifyId);
        youmuNotify.setReadNum(youmuNotify.getReadNum()+1);
        notifyDao.upload(youmuNotify);

        //
        YoumuNotifyReadHistory youmuNotifyReadHistory = new YoumuNotifyReadHistory();
        youmuNotifyReadHistory.setAddTime((Timestamp) new Date());
        youmuNotifyReadHistory.setFundUserId(fundUserId);
        youmuNotifyReadHistory.setNotifyId(notifyId);
        youmuNotifyReadHistory.setReadTime((Timestamp) new Date());
        youmuNotifyReadHistory.setIsDelete(false);

        return notifyReadDao.insert(youmuNotifyReadHistory);
    }

    @Override
    public YoumuNotify findById(Long id) {

        return notifyDao.findById(id);
    }

    @Override
    public PageT findByFilter(int num, int size, int type) {
        return  notifyDao.findByFilter( num, size,  type);
    }

}
